package tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class WebGet {

	public static String getPage(String url)	{
		String page = "";
		
		URL pageAddress;
		try {
			pageAddress = new URL(url);
	        URLConnection yc = pageAddress.openConnection();
	        InputStreamReader isr = new InputStreamReader(yc.getInputStream());
	        BufferedReader in = new BufferedReader(isr);
	        String inputLine;

	        while( (inputLine = in.readLine()) != null ) 
	            page += "\n" + inputLine;

	        in.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return page;
	}
	
	public static String stripHTML(String input)	{
		String[] htmlTags = { "&nbsp;",
							  "<b>",
							  "</b>",
							  "<span>",
							  "</span>"};
		
		
		for(int x=0; x < htmlTags.length; x++)	{
			if( input.contains(htmlTags[x]) )	{
				input = input.replace(htmlTags[x], "\010");
			}
		}

		
		// REMOVE BACKSPACE CHARS WE PUT IN!
		String[] clean = input.split("\010");

		String output = "";
		
		for(int x=0; x < clean.length; x++)
			output += clean[x];
		// END CHAR REMOVAL 

		// output is now free of tags included in the htmlTags[]
		return output;
	}
	
	public static String urlSafeString(String s)	{
		
		s = s.replace(" ", "%20");
		System.out.println("URL SAFE STRING: " + s);
		return s;
	}
	
}
